﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace OnlineDovizOtomasyonu
{
    public partial class genelhesap : System.Web.UI.Page
    {
        Fonksiyon Sistem = new Fonksiyon();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
            {
                int Gun = 0, Ay = 0, Yil = 0;
                DateTime YeniTarih1;
                Gun = Convert.ToInt32(DateTime.Now.Day);
                Ay = Convert.ToInt32(DateTime.Now.Month);
                Yil = Convert.ToInt32(DateTime.Now.Year)-1;
                YeniTarih1 = Convert.ToDateTime(Gun + "." + Ay + "." + Yil);
                txtTarih1.Value = YeniTarih1;
                txtTarih2.Value = DateTime.Today;
                AFonksiyonToplam();
                VFonksiyonToplam();
                TFonksiyonToplam();
                KurCek();
                TLDonusum();
                USDDonusum();
                EURODonusum();
                SARDonusum();
                SRYDonusum();
                TextGrafikKontrol();
                
            }
        }

        void AFonksiyonToplam()
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "SELECT SUM(ATL) AS ATLTOPLAM FROM VWTRHTL WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                txtATL.Text = dr["ATLTOPLAM"].ToString();
            }

            SqlConnection con1 = Sistem.baglan();
            SqlCommand komut1 = new SqlCommand();
            komut1.Connection = con1;
            komut1.CommandText = "SELECT SUM(AUSD) AS AUSDTOPLAM FROM VWTRHUSD WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr1 = komut1.ExecuteReader();
            while (dr1.Read())
            {
                txtAUSD.Text = dr1["AUSDTOPLAM"].ToString();
            }

            SqlConnection con2 = Sistem.baglan();
            SqlCommand komut2 = new SqlCommand();
            komut2.Connection = con2;
            komut2.CommandText = "SELECT SUM(AEURO) AS AEUROTOPLAM FROM VWTRHEURO WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr2 = komut2.ExecuteReader();
            while (dr2.Read())
            {
                txtAEURO.Text = dr2["AEUROTOPLAM"].ToString();
            }

            SqlConnection con3 = Sistem.baglan();
            SqlCommand komut3 = new SqlCommand();
            komut3.Connection = con3;
            komut3.CommandText = "SELECT SUM(ASAR) AS ASARTOPLAM FROM VWTRHSAR WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr3 = komut3.ExecuteReader();
            while (dr3.Read())
            {
                txtASAR.Text = dr3["ASARTOPLAM"].ToString();
            }

            SqlConnection con4 = Sistem.baglan();
            SqlCommand komut4 = new SqlCommand();
            komut4.Connection = con4;
            komut4.CommandText = "SELECT SUM(ASRY) AS ASRYTOPLAM FROM VWTRHSRY WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr4 = komut4.ExecuteReader();
            while (dr4.Read())
            {
                txtASRY.Text = dr4["ASRYTOPLAM"].ToString();
            }
        }
        void VFonksiyonToplam()
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "SELECT SUM(VTL) AS VTLTOPLAM FROM VWTRHTL WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                txtVTL.Text = dr["VTLTOPLAM"].ToString();
            }

            SqlConnection con1 = Sistem.baglan();
            SqlCommand komut1 = new SqlCommand();
            komut1.Connection = con1;
            komut1.CommandText = "SELECT SUM(VUSD) AS VUSDTOPLAM FROM VWTRHUSD WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr1 = komut1.ExecuteReader();
            while (dr1.Read())
            {
                txtVUSD.Text = dr1["VUSDTOPLAM"].ToString();
            }

            SqlConnection con2 = Sistem.baglan();
            SqlCommand komut2 = new SqlCommand();
            komut2.Connection = con2;
            komut2.CommandText = "SELECT SUM(VEURO) AS VEUROTOPLAM FROM VWTRHEURO WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr2 = komut2.ExecuteReader();
            while (dr2.Read())
            {
                txtVEURO.Text = dr2["VEUROTOPLAM"].ToString();
            }

            SqlConnection con3 = Sistem.baglan();
            SqlCommand komut3 = new SqlCommand();
            komut3.Connection = con3;
            komut3.CommandText = "SELECT SUM(VSAR) AS VSARTOPLAM FROM VWTRHSAR WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr3 = komut3.ExecuteReader();
            while (dr3.Read())
            {
                txtVSAR.Text = dr3["VSARTOPLAM"].ToString();
            }

            SqlConnection con4 = Sistem.baglan();
            SqlCommand komut4 = new SqlCommand();
            komut4.Connection = con4;
            komut4.CommandText = "SELECT SUM(VSRY) AS VSRYTOPLAM FROM VWTRHSRY WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr4 = komut4.ExecuteReader();
            while (dr4.Read())
            {
                txtVSRY.Text = dr4["VSRYTOPLAM"].ToString();
            }
        }
        void TFonksiyonToplam()
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "SELECT SUM(TTL) AS TTLTOPLAM FROM VWTRHTL WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                txtTTL.Text = dr["TTLTOPLAM"].ToString();
            }

            SqlConnection con1 = Sistem.baglan();
            SqlCommand komut1 = new SqlCommand();
            komut1.Connection = con1;
            komut1.CommandText = "SELECT SUM(TUSD) AS TUSDTOPLAM FROM VWTRHUSD WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr1 = komut1.ExecuteReader();
            while (dr1.Read())
            {
                txtTUSD.Text = dr1["TUSDTOPLAM"].ToString();
            }

            SqlConnection con2 = Sistem.baglan();
            SqlCommand komut2 = new SqlCommand();
            komut2.Connection = con2;
            komut2.CommandText = "SELECT SUM(TEURO) AS TEUROTOPLAM FROM VWTRHEURO WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr2 = komut2.ExecuteReader();
            while (dr2.Read())
            {
                txtTEURO.Text = dr2["TEUROTOPLAM"].ToString();
            }

            SqlConnection con3 = Sistem.baglan();
            SqlCommand komut3 = new SqlCommand();
            komut3.Connection = con3;
            komut3.CommandText = "SELECT SUM(TSAR) AS TSARTOPLAM FROM VWTRHSAR WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr3 = komut3.ExecuteReader();
            while (dr3.Read())
            {
                txtTSAR.Text = dr3["TSARTOPLAM"].ToString();
            }

            SqlConnection con4 = Sistem.baglan();
            SqlCommand komut4 = new SqlCommand();
            komut4.Connection = con4;
            komut4.CommandText = "SELECT SUM(TSRY) AS TSRYTOPLAM FROM VWTRHSRY WHERE TARIH BETWEEN '" + txtTarih1.Value + "' AND '" + txtTarih2.Value + "'";
            SqlDataReader dr4 = komut4.ExecuteReader();
            while (dr4.Read())
            {
                txtTSRY.Text = dr4["TSRYTOPLAM"].ToString();
            }
        }
        void KurCek()
        {
            SqlConnection con = Sistem.baglan();
            SqlCommand komut = new SqlCommand();
            komut.Connection = con;
            komut.CommandText = "SELECT * FROM TBLKUR";
            SqlDataReader dr = komut.ExecuteReader();
            while (dr.Read())
            {
                string TL = "", USD = "", EURO = "", SAR = "", SRY = "";
                TL = dr["TL"].ToString();
                USD = dr["USD"].ToString();
                EURO = dr["EURO"].ToString();
                SAR = dr["SAR"].ToString();
                SRY = dr["SRY"].ToString();
                if (TL=="")
                {
                    txtKTL.Text = "0,00";
                }
                else
                {
                    txtKTL.Text = TL;
                }
                if (USD == "")
                {
                    txtKUSD.Text = "0,00";
                }
                else
                {
                    txtKUSD.Text = USD;
                }
                if (EURO == "")
                {
                    txtKEURO.Text = "0,00";
                }
                else
                {
                    txtKEURO.Text = EURO;
                }
                if (SAR == "")
                {
                    txtKSAR.Text = "0,00";
                }
                else
                {
                    txtKSAR.Text = SAR;
                }
                if (SRY == "")
                {
                    txtKSRY.Text = "0,00";
                }
                else
                {
                    txtKSRY.Text = SRY;
                }
            }
        }
        void TLDonusum()
        {
            decimal TLToplami = Convert.ToDecimal(txtTTL.Text);
            decimal USDTLCevrimi = Convert.ToDecimal(txtTUSD.Text) * Convert.ToDecimal(txtKUSD.Text);
            decimal EUROTLCevrimi = Convert.ToDecimal(txtTEURO.Text) * Convert.ToDecimal(txtKEURO.Text);
            decimal SARTLCevrimi = Convert.ToDecimal(txtTSAR.Text) * Convert.ToDecimal(txtKSAR.Text);
            decimal SRYTLCevrimi = Convert.ToDecimal(txtTSRY.Text) * Convert.ToDecimal(txtKSRY.Text);
            txtGTL.Text = Convert.ToDecimal(TLToplami + USDTLCevrimi + EUROTLCevrimi + SARTLCevrimi + SRYTLCevrimi).ToString("N2");
        }
        void USDDonusum()
        {
            decimal USDToplami = Convert.ToDecimal(txtTUSD.Text);
            decimal TLUSDCevrimi = Convert.ToDecimal(txtTTL.Text) / Convert.ToDecimal(txtKUSD.Text);
            decimal EUROUSDCevrimi = Convert.ToDecimal(txtTEURO.Text) * Convert.ToDecimal(txtKEURO.Text) / Convert.ToDecimal(txtKUSD.Text);
            decimal SARUSDCevrimi = Convert.ToDecimal(txtTSAR.Text) * Convert.ToDecimal(txtKSAR.Text) / Convert.ToDecimal(txtKUSD.Text);
            decimal SRYUSDCevrimi = Convert.ToDecimal(txtTSRY.Text) * Convert.ToDecimal(txtKSRY.Text) / Convert.ToDecimal(txtKUSD.Text);
            txtGUSD.Text = Convert.ToDecimal(USDToplami + TLUSDCevrimi + EUROUSDCevrimi + SARUSDCevrimi + SRYUSDCevrimi).ToString("N2");
        }
        void EURODonusum()
        {
            decimal EUROToplami = Convert.ToDecimal(txtTEURO.Text);
            decimal TLEUROCevrimi = Convert.ToDecimal(txtTTL.Text) / Convert.ToDecimal(txtKEURO.Text);
            decimal USDEUROCevrimi = Convert.ToDecimal(txtTUSD.Text) * Convert.ToDecimal(txtKUSD.Text) / Convert.ToDecimal(txtKEURO.Text);
            decimal SAREUROCevrimi = Convert.ToDecimal(txtTSAR.Text) * Convert.ToDecimal(txtKSAR.Text) / Convert.ToDecimal(txtKEURO.Text);
            decimal SRYEUROCevrimi = Convert.ToDecimal(txtTSRY.Text) * Convert.ToDecimal(txtKSRY.Text) / Convert.ToDecimal(txtKEURO.Text);
            txtGEURO.Text = Convert.ToDecimal(EUROToplami + TLEUROCevrimi + USDEUROCevrimi + SAREUROCevrimi + SRYEUROCevrimi).ToString("N2");
        }

        void SARDonusum()
        {
            decimal GTLToplami = Convert.ToDecimal(txtGTL.Text);
            txtGSAR.Text = Convert.ToDecimal(GTLToplami / Convert.ToDecimal(txtKSAR.Text)).ToString("N2");
        }

        void SRYDonusum()
        {
            decimal GTLToplami = Convert.ToDecimal(txtGTL.Text);
            txtGSRY.Text = Convert.ToDecimal(GTLToplami / Convert.ToDecimal(txtKSRY.Text)).ToString("N2");
        }

        protected void txtTarih1_DateChanged(object sender, EventArgs e)
        {
            AFonksiyonToplam();
            VFonksiyonToplam();
            TFonksiyonToplam();
            TLDonusum();
            USDDonusum();
            EURODonusum();
            SARDonusum();
            SRYDonusum();
            TextGrafikKontrol();
        }

        protected void txtTarih2_DateChanged(object sender, EventArgs e)
        {
            AFonksiyonToplam();
            VFonksiyonToplam();
            TFonksiyonToplam();
            TLDonusum();
            USDDonusum();
            EURODonusum();
            SARDonusum();
            SRYDonusum();
            TextGrafikKontrol();
        }

        void TextGrafikKontrol()
        {
            if (Convert.ToDecimal(txtATL.Text) <=0)
            {
                ATLImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                ATLImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtAUSD.Text) <=0)
            {
                AUSDImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                AUSDImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtAEURO.Text) <=0)
            {
                AEUROImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                AEUROImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtASAR.Text) <=0)
            {
                ASARImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                ASARImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtASRY.Text) <=0)
            {
                ASRYImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                ASRYImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtVTL.Text) <=0)
            {
                VTLImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                VTLImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtVUSD.Text) <=0)
            {
                VUSDImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                VUSDImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtVEURO.Text) <=0)
            {
                VEUROImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                VEUROImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtVSAR.Text) <=0)
            {
                VSARImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                VSARImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtVSRY.Text) <=0)
            {
                VSRYImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                VSRYImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtTTL.Text) <= 0)
            {
                TTLImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                TTLImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtTUSD.Text) <= 0)
            {
                TUSDImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                TUSDImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtTEURO.Text) <= 0)
            {
                TEUROImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                TEUROImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtTSAR.Text) <= 0)
            {
                TSARImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                TSARImage.ImageUrl = "~/images/icon/up.gif";
            }

            if (Convert.ToDecimal(txtTSRY.Text) <= 0)
            {
                TSRYImage.ImageUrl = "~/images/icon/down.gif";
            }
            else
            {
                TSRYImage.ImageUrl = "~/images/icon/up.gif";
            }
        }

        protected void txtATL_TextChanged(object sender, EventArgs e)
        {
           
            
        }
    }
}